<?php
class Boom extends CI_Controller{
        private $limit = 10;
	function  __construct() {
        parent::__construct();
        $this->load->model('Boom_model');
	$this->load->helper(array('form', 'url'));
	$this->load->library(array('form_validation', 'pagination'));
    }

    function index($offset=''){
                $this->load->library('pagination'); //load library pagination
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('boom/index/');
		$config['total_rows'] = $this->Boom_model->countData();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		$data['jumlah_data']=$this->Boom_model->countData();
		$data['boom']	= $this->Boom_model->get_list_data($this->limit,$offset);

	//$data['boom'] = $this->Boom_model->getBoom();

        $this->load->view('admin_views/boom/boom', $data);

    }
	 function lookup(){
	$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Boom_model->selectDealer($keyword);
        if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                $data['message'][] = array(
                                        'id'=>$row->intid_dealer,
                                        'value' => $row->strnama_dealer,
                                        ''
                                     );
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data);
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	function add()
	{
		$this->form_validation->set_rules('intid_tipeboom', 'Tipe Boom', 'trim|required|xss_clean');
		$this->form_validation->set_rules('intid_dealer', 'Dealer', 'trim|required|xss_clean');
		$this->form_validation->set_rules('intid_omset', 'Omset', 'trim|required|xss_clean');


		$this->form_validation->set_message('required', ' %s tidak boleh kosong !!');

		if ($this->form_validation->run() == FALSE)
		{
			$tboom= $this->Boom_model->selectTboom();
                       foreach ($tboom as $g)

			{
				$data['id'][]	 	= $g->intid_tipeboom;
				$data['nama'][] 	= $g->inttotal;
			}
           /* $dealer = $this->Boom_model->selectDealer();
			foreach ($dealer as $g)
			{
				$data['idd'][]          = $g->intid_dealer;
				$data['namad'][] 	= $g->strnama_dealer;
			}*/
                        $omset = $this->Boom_model->selectOmset();
			foreach ($omset as $g)
			{
				$data['idset'][]	= $g->intid_omset;
				$data['namaset'][] 	= $g->intomset;
			}
			$this->load->view('admin_views/boom/add', $data);

		}else {
			$this->Boom_model->insert($_POST);
			redirect('boom/index');
		}
	}
		function edit($id)
		{
			if($_POST==NULL)
			{
				$tboom= $this->Boom_model->selectTboom();
                                foreach ($tboom as $g)
			{
				$data['id'][]	 	= $g->intid_tipeboom;
				$data['nama'][] 	= $g->inttotal;
			}
                        $dealer = $this->Boom_model->selectDealer();
			foreach ($dealer as $g)
			{
				$data['idd'][]          = $g->intid_dealer;
				$data['namad'][] 	= $g->strnama_dealer;
			}
                        $omset = $this->Boom_model->selectOmset();
			foreach ($omset as $g)
			{
				$data['idset'][]	= $g->intid_omset;
				$data['namaset'][] 	= $g->intomset;
			}

				$databoom = $this->Boom_model->select($id);
				foreach ($databoom as $g)
				{
					$data['intid_boom'] 		= $g->intid_boom;
					$data['intid_tipeboom']		= $g->intid_tipeboom;
					$data['intid_dealer']	 	= $g->intid_dealer;
					$data['intid_omset'] 		= $g->intid_omset;
					
				}
				$this->load->view('admin_views/boom/edit', $data);
			}else {

				$this->Boom_model->update($id);
				redirect('boom/index');
			}

		}

	function delete($id)
	{
		$this->Boom_model->delete($id);
		redirect('boom/index');
	}

   
}
?>
